package com.wisdytech.linkdcs.equipLifeCycle.check.action;


import java.util.List;

import com.wisdytech.common.handler.ResourceMatchingService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.bean.StandardTableResult;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.common.utils.PojoConvertUtil;
import com.wisdytech.linkdcs.equipLifeCycle.check.model.CheckModelItem;
import com.wisdytech.linkdcs.equipLifeCycle.check.service.ICheckModelItemService;
import com.wisdytech.linkdcs.equipLifeCycle.check.vo.CheckItemVo;
import com.wisdytech.linkdcs.equipLifeCycle.check.vo.CheckModelItemVo;
import com.wisdytech.linkdcs.system.model.SysUser;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 
 *  点检项目
 *  前端控制器
 * </p>
 *
 * @author dailibo
 * @since 2018-06-05
 */
@Controller
@RequestMapping("/equipLifeCycle/check/checkModelItem")
public class CheckModelItemController {

	private Logger logger =LoggerFactory.getLogger(CheckModelItemController.class);
	
	@Autowired
	private ICheckModelItemService iCheckModelItemService;
    @Autowired
    @Qualifier("resourceMatchingService")
    private ResourceMatchingService resourceMatchingService;
	
	@RequestMapping("/index")
	public String itemIndex(Model model, HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
		return "lifeCycle/equipLifeCycle/check/itemIndex";
	}
	
	@GetMapping("/listItems/get.mvc")
	@ResponseBody
	public StandardTableResult<CheckModelItemVo> listCheckModelItems(
			Filter filter,String modelCode){	
		PageInfo<CheckModelItem> items=
				iCheckModelItemService.listCheckModelItems(filter, modelCode);
		
		List<CheckModelItemVo>  itemVos =
				PojoConvertUtil.convertPojos(items.getList(), CheckModelItemVo.class);
		
		
		return new StandardTableResult<>(items.getTotal(),itemVos);
	}
	
	@GetMapping("/listAllItems/get.mvc")
	@ResponseBody
	public StandardTableResult<CheckModelItemVo> listAllCheckModelItems(String pkId){
		if(StringUtils.isBlank(pkId)) {
			return new StandardTableResult<CheckModelItemVo>();
		}
		
		Filter filter =new Filter();
		filter.setNum(1);
		filter.setSize(Integer.MAX_VALUE);
		
		PageInfo<CheckModelItem> items=
				iCheckModelItemService.listCheckModelItemsByEquip(filter, pkId);
		List<CheckModelItemVo>  itemVos =
				PojoConvertUtil.convertPojos(items.getList(), CheckModelItemVo.class);
		
		
		return new StandardTableResult<>(items.getTotal(),itemVos);
		
	}
	

	@PostMapping("/delItems/del.mvc")
	@ResponseBody
	public StandardResult delCheckItems(@ModelAttribute("pkId")String pkId) {
		SysUser sysUser =BaseUtil.getCurrentUser();
		
		return iCheckModelItemService.del(pkId, sysUser.getUsername())?
				StandardResult.getSuccessResult():StandardResult.getFailResult("删除错误");
	}
	
	@PostMapping("/addOrUpdateItem/add.mvc")
	@ResponseBody
	public StandardResult addCheckItem(CheckModelItem item) {
		
		SysUser sysUser =BaseUtil.getCurrentUser();
		boolean result=false;
		if(StringUtils.isBlank(item.getPkId())) {
			result=iCheckModelItemService.add(item, sysUser.getUsername());
		}else {
			result=iCheckModelItemService.update(item, sysUser.getUsername());
		}
		return result?
				StandardResult.getSuccessResult():StandardResult.getFailResult("新增/修改失败");
	}
	
	@GetMapping("/getItem/get.mvc")
	@ResponseBody
	public StandardResult getCheckItem(String pkId) {
		if(StringUtils.isBlank(pkId)) {
			return StandardResult.getFailResult("唯一id为空");
		}
		
		CheckModelItem item=
				iCheckModelItemService.getItemById(pkId);
		if(null == item) {
			return StandardResult.getFailResult("点检项目为空");
		}
		
		return StandardResult.getSuccessResult(
				PojoConvertUtil.convertPojo(item, CheckItemVo.class));
	}
	
}

